package jv.redis.datatype.list;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;

public class RedisList {
	Jedis jedis;
	@Before
	public void initRedis(){
		jedis = new Jedis("redis://192.168.1.108:6379");
	}
	
	@After
	public void closeRedis(){
		//注意不要使用jedis.shutdown()，这样会把服务器停掉。。。
		jedis.close();
	}
	/**
	 * 下面命令的中文解释请参考https://my.oschina.net/u/3049601/blog/1162577
	 */
	@Test
	public void listOptions(){
		jedis.del("person:id");
		jedis.lpush("person:id", "1", "2", "3","3", "2", "1");
		System.out.println("after lpush:"+jedis.lrange("person:id", 0, -1));
		
		jedis.rpush("person:id", "4", "5", "6","6", "5", "4");
		System.out.println("after rpush:"+jedis.lrange("person:id", 0, -1));
		
		System.out.println("lpop result:"+jedis.lpop("person:id"));
		System.out.println("after lpop:"+jedis.lrange("person:id", 0, -1));
		
		System.out.println("rpop result:"+jedis.rpop("person:id"));
		System.out.println("after rpop:"+jedis.lrange("person:id", 0, -1));
		
		System.out.println("person:id length="+jedis.llen("person:id"));
		
		System.out.println("lrem by left result:"+jedis.lrem("person:id", 1, "3"));
		System.out.println("after lrem by left:"+jedis.lrange("person:id", 0, -1));
		
		System.out.println("lrem by right result:"+jedis.lrem("person:id", -1, "5"));
		System.out.println("after lrem by right:"+jedis.lrange("person:id", 0, -1));
		
		jedis.linsert("person:id", LIST_POSITION.BEFORE, "6", "7");
		System.out.println("after linsert:"+jedis.lrange("person:id", 0, -1));
		
		jedis.ltrim("person:id", -2, -1);
		System.out.println("after ltrim:"+jedis.lrange("person:id", 0, -1));
	}
	
	/**
	 * 已经学习过散列和列表两种数据类型，现在可以写一段代码来存放一组对象及其属性
	 */
	@Test
	public void listOptionsExtends(){
		Long personId;
		String rPersonId;
		Map<String,String> personMap = new HashMap<String,String>();
		//为了测试方便，在每次程序启动的时候都把之前的元素删除，在实际编程中谨慎使用
		jedis.del("person:id");
		jedis.del("person:id:seq");
		
		//添加第一个人
		personId = jedis.incr("person:id:seq");
		rPersonId = "person:"+personId.toString();
		jedis.lpush("person:id", personId.toString());
		personMap.put("name", "lucy");
		personMap.put("sex","female");
		personMap.put("age","23");
		jedis.hmset(rPersonId,personMap);
		personMap.clear();
		
		//添加第二个人
		personId = jedis.incr("person:id:seq");
		rPersonId = "person:"+personId.toString();
		jedis.lpush("person:id", personId.toString());
		personMap.put("name", "hary");
		personMap.put("sex","male");
		personMap.put("age","25");
		jedis.hmset(rPersonId,personMap);
		personMap.clear();
		
		//添加第三个人
		personId = jedis.incr("person:id:seq");
		rPersonId = "person:"+personId.toString();
		jedis.lpush("person:id", personId.toString());
		personMap.put("name", "volk");
		personMap.put("sex","male");
		personMap.put("age","25");
		jedis.hmset(rPersonId,personMap);
		personMap.clear();
		
		System.out.println(jedis.lrange("person:id", 0, -1));
		System.out.println(jedis.hgetAll("person:1"));
		System.out.println(jedis.hgetAll("person:2"));
		System.out.println(jedis.hgetAll("person:3"));
	}
}
